package subject_set.offer100;

/**
 * @author haomin
 * @date 2022/10/21 21:12
 **/
public class Offer53 {
    class Solution {
        public int search(int[] nums, int target) {
            int l = 0, r = nums.length-1, mid;
            while(l < r){
                mid = l + ((r-l)>>1);
                if(nums[mid] >= target){
                    r = mid;
                }else if(nums[mid] < target){
                    l = mid + 1;
                }
            }
            int count = 0;
            while(l < nums.length && nums[l] == target){
                count++;
                l++;
            }
            return count;
        }
    }

    // 简化
    class Solution2 {
        public int search(int[] nums, int target) {
            int l = 0, r = nums.length-1, mid;
            while(l < r){
                mid = l + ((r-l)>>1);
                if(nums[mid] >= target) r = mid;
                else l = mid + 1;
            }
            int count = 0;
            while(l < nums.length && nums[l++] == target) count++;
            return count;
        }
    }
}